#include "stdafx.h"
#include "qteditor.h"

QTEditor::QTEditor(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	// base init
	bEnableCameraControl = false;

	// Create user interface
	editorUI.Create(this);
	m_renderFrame = editorUI.glFrame;

	// create loop timer
	m_loopTimer = new QTimer(this);
	this->connect(m_loopTimer, SIGNAL(timeout()), m_renderFrame, SLOT(updateGL()));
	m_loopTimer->start(15);
}



QTEditor::~QTEditor()
{

}

void QTEditor::keyPressEvent(QKeyEvent *evt)
{
	static float speed = 10.0f;

	switch(evt->key())
	{
	case Qt::Key_Escape:
		this->close();
		break;
	case Qt::Key_W:  
		editorUI.glFrame->m_engine.qCamera.move(-speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;
         
    case Qt::Key_S:  
		editorUI.glFrame->m_engine.qCamera.move(speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;         
         
    case Qt::Key_A:  
		editorUI.glFrame->m_engine.qCamera.strafe(-speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
      break;
         
    case Qt::Key_D:  
		editorUI.glFrame->m_engine.qCamera.strafe(speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
      break;           

	case Qt::Key_R:
		editorUI.glFrame->m_engine.gCamera.Pitch(speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_F:
		editorUI.glFrame->m_engine.gCamera.Pitch(-speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_Right:
		//editorUI.glFrame->m_engine.unit.Strafe(-speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_Up:
		//editorUI.glFrame->m_engine.unit.Move(speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_Left:
		//editorUI.glFrame->m_engine.unit.Strafe(speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_Down:
		//editorUI.glFrame->m_engine.unit.Move(-speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_U:
		//editorUI.glFrame->m_engine.unit.Yaw(-speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	case Qt::Key_I:
		//editorUI.glFrame->m_engine.unit.Yaw(speed * editorUI.glFrame->m_engine.m_timer.GetDeltaTime());
		break;

	}

	//editorUI.glFrame->updateGL();
}



void QTEditor::mouseMoveEvent(QMouseEvent* pe)
{
	static bool first = true;
	static int x=0, y=0;

	/*if(first)
	{
		x = pe->x();
		y = pe->y();
		first = false;
		return;
	}*/

	if(bEnableCameraControl)
	{
		//editorUI.glFrame->m_engine.gCamera.Yaw(( mouseX - pe->x()) * 0.005f);
		//editorUI.glFrame->m_engine.gCamera.Pitch(( mouseY - pe->y()) * 0.005f);
		
		editorUI.glFrame->m_engine.qCamera.pitch((mouseY - pe->y()) * -0.005f);
		editorUI.glFrame->m_engine.qCamera.yaw((mouseX - pe->x()) * -0.005f);
		mouseX = pe->x();
		mouseY = pe->y();
	}
	
	//mouseX = pe->x();
	//mouseY = pe->y();
}

void QTEditor::mousePressEvent(QMouseEvent *evt)
{
	switch(evt->button())
	{
	case Qt::LeftButton:
		bEnableCameraControl = true;
		mouseX = evt->x();
		mouseY = evt->y();
		break;
	}
}

void QTEditor::mouseReleaseEvent(QMouseEvent *evt)
{
	switch(evt->button())
	{
	case Qt::LeftButton:
		bEnableCameraControl = false;
		break;
	}
}
